package com.example.library_common.view.video_view;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.library_common.R;
import com.example.library_common.glidmodule.GlideApp;
import com.example.library_common.view.ImageProgress;
import com.example.library_common.view.video_view.utils.VideoViewControllerListener;

public class VideoViewCenterControllerView extends FrameLayout implements View.OnClickListener {

    private ImageProgress mVideoBrightnessAvatar, mVideoVolumeAvatar;

    private float lastX = 0, lastY = 0;

    private float threshold = 80;

    private boolean isAdjust = false;

    private float viewHeight = 0f;

    private float viewWidth = 0f;

    private float currentVolume;

    private float maxVolume;

    private AudioManager audioManager;

    private float screenBrightness;

    private ImageView mVideoViewMaskedImg;

    private View mVideoViewMask;

    private View mVideoViewLoading;

    private View mVideoViewErrorLayout;

    private VideoViewControllerListener mVideoViewControllerListener;

    public void setVideoViewControllerListener( VideoViewControllerListener mVideoViewControllerListener) {
        this.mVideoViewControllerListener = mVideoViewControllerListener;
    }


    public VideoViewCenterControllerView(Context context) {
        this(context, null);
    }

    public VideoViewCenterControllerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VideoViewCenterControllerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        inflate(context, R.layout.video_view_center_controller_view_layout, this);

        mVideoViewErrorLayout=findViewById(R.id.mVideoViewErrorLayout);

        TextView mVideoViewErrorStatus=findViewById(R.id.mVideoViewErrorStatus);

        mVideoViewLoading = findViewById(R.id.mVideoViewLoading);

        mVideoViewMask = findViewById(R.id.mVideoViewMask);

        mVideoViewMaskedImg = findViewById(R.id.mVideoViewMaskedImg);

        mVideoBrightnessAvatar = findViewById(R.id.mVideoBrightnessAvatar);
        mVideoVolumeAvatar = findViewById(R.id.mVideoVolumeAvatar);

        audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

        mVideoVolumeAvatar.setProgress(currentVolume / maxVolume);

        mVideoViewErrorStatus.setOnClickListener(this);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        viewWidth = MeasureSpec.getSize(widthMeasureSpec);
        viewHeight = MeasureSpec.getSize(heightMeasureSpec);
        threshold = viewHeight / 20f;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewWidth = w;
        viewHeight = h;
    }

    public void setCoverUrl(String coverUrl) {
        GlideApp.with(this)
                .load(coverUrl)
                .placeholder(R.mipmap.image_loading)
                .error(R.drawable.img_erros_bg_rect)
                .into(mVideoViewMaskedImg);
    }

    public ImageView getVideoViewMaskedImg() {
        return mVideoViewMaskedImg;
    }

    public void showMask(boolean isShowLoading) {
        mVideoViewMask.setVisibility(VISIBLE);
        mVideoViewErrorLayout.setVisibility(GONE);
        if (isShowLoading) {
            mVideoViewLoading.setVisibility(VISIBLE);
        } else {

            mVideoViewLoading.setVisibility(GONE);
        }
    }

    public void hidMask() {
        mVideoViewMask.setVisibility(GONE);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = x;
                lastY = y;

                break;

            case MotionEvent.ACTION_MOVE:

                float deltaX = x - lastX;
                float deltaY = y - lastY;

                float absDX = Math.abs(deltaX);
                float absDY = Math.abs(deltaY);

                if (absDX > threshold && absDY > threshold) {
                    isAdjust = absDX < absDY;
                }
                if (isAdjust) {
                    if (x > viewWidth / 2) {
                        mVideoVolumeAvatar.setVisibility(View.GONE);
                        handleBrightness(-deltaY);
                    } else {
                        mVideoBrightnessAvatar.setVisibility(View.GONE);
                        handleVolume(-deltaY);
                    }
                    lastY = y;
                }

                break;
            case MotionEvent.ACTION_UP:
                mVideoBrightnessAvatar.setVisibility(View.GONE);
                mVideoVolumeAvatar.setVisibility(View.GONE);
                screenBrightness = -1;

                break;
        }
        return true;
    }


    private void handleVolume(float deltaY) {

        mVideoVolumeAvatar.setVisibility(VISIBLE);

        float radios = Math.min(deltaY, viewHeight) / Math.max(deltaY, viewHeight);

        float deltaV = currentVolume / maxVolume + radios;
//
        currentVolume = maxVolume * deltaV;

        if (currentVolume < 0f) {
            currentVolume = 0f;
        } else if (currentVolume > maxVolume) {
            currentVolume = maxVolume;
        }

        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, (int) currentVolume, 0);
        float volumePercent = currentVolume / maxVolume;
        mVideoVolumeAvatar.setProgress(volumePercent);
    }


    private void handleBrightness(float dY) {
        mVideoBrightnessAvatar.setVisibility(VISIBLE);
        Activity activity = (Activity) getContext();
        Window window = activity.getWindow();
        if (window == null) {
            return;
        }
        WindowManager.LayoutParams params = window.getAttributes();
        screenBrightness = params.screenBrightness;
        mVideoBrightnessAvatar.setProgress(screenBrightness);
        float percent = (dY / viewHeight);

        screenBrightness += percent;
        if (screenBrightness > 1.0f) {
            screenBrightness = 1.0f;
        } else if (screenBrightness < 0.01f) {
            screenBrightness = 0.01f;
        }

        params.screenBrightness = screenBrightness;
        window.setAttributes(params);
        mVideoBrightnessAvatar.setProgress(screenBrightness);
    }

    public void showMaskWhenError() {
        mVideoViewMask.setVisibility(VISIBLE);
        mVideoViewLoading.setVisibility(GONE);
        mVideoViewErrorLayout.setVisibility(VISIBLE);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.mVideoViewErrorStatus) {
            mVideoViewMask.setVisibility(VISIBLE);
            mVideoViewLoading.setVisibility(VISIBLE);
            mVideoViewErrorLayout.setVisibility(GONE);
            if (mVideoViewControllerListener != null) {
                mVideoViewControllerListener.onVideoViewReloading();
            }
        }
    }

}
